package com.lonely.framework.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.lonely.framework.handler.MyMetaObjectHandler;
import com.lonely.framework.handler.MyDataPermissionHandler;
import com.lonely.framework.interceptor.MyDataPermissionInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis plus 配置
 *
 * @author fzzf
 */
@Configuration
public class MybatisPlusConfig {

//    /**
//     * 分页插件
//     */
//    @Bean
//    public PaginationInnerInterceptor paginationInterceptor() {
//        return new PaginationInnerInterceptor(DbType.MYSQL);
//    }


    /**
     * 元数据配置
     *
     * @return
     */
    @Bean
    public MyMetaObjectHandler myMetaObjectHandler() {
        return new MyMetaObjectHandler();
    }


    /**
     * mybatis plus 拦截器
     * @return
     */
    @Bean
    public Interceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加数据权限插件
        MyDataPermissionInterceptor dataPermissionInterceptor = new MyDataPermissionInterceptor();
        // 添加自定义的数据权限处理器
        dataPermissionInterceptor.setDataPermissionHandler(new MyDataPermissionHandler());
        interceptor.addInnerInterceptor(dataPermissionInterceptor);

        // 分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setOptimizeJoin(false); // 关闭该优化，否则会出来分页总数量统计错误
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }
}

